package org.vehicle.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.vehicle.dao.AlertInfoMapper;
import org.vehicle.dao.DataRecordInfoMapper;
import org.vehicle.entity.AlertInfo;
import org.vehicle.entity.DataRecordInfo;
import org.vehicle.entity.PersonInfo;
import org.vehicle.service.AlertInfoService;
import org.vehicle.service.base.BaseServiceImpl;

@Service
public class AlertInfoServiceImpl extends BaseServiceImpl<AlertInfo, Long> implements AlertInfoService{
	@Autowired
	private AlertInfoMapper alertInfoMapper;
	@Autowired
	private DataRecordInfoMapper dataRecordInfoMapper;
	@Override
	public List<AlertInfo> findAlertInfoByPersonInfoList(List<PersonInfo> personInfoList) {
		
		return alertInfoMapper.findAlertInfoByPersonInfoList(personInfoList);
	}

	@Override
	public AlertInfo findAlertInfoByPersonIdAndAlertType(Map<String, Object> map) {
		
		return alertInfoMapper.findAlertInfoByPersonIdAndAlertType(map);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void addOrUpdateAlertInfo(AlertInfo alertInfo,PersonInfo personInfo) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("personInfoId", personInfo.getPersonInfoId());
		map.put("alertType", 1);
		AlertInfo tempAlertInfo=alertInfoMapper.findAlertInfoByPersonIdAndAlertType(map);
		if(tempAlertInfo==null){
			alertInfoMapper.insertSelective(alertInfo);
		}else{
			alertInfo.setAlertInfoId(tempAlertInfo.getAlertInfoId());
			alertInfoMapper.updateByPrimaryKeySelective(alertInfo);
		}
		DataRecordInfo dataRecordInfo=new DataRecordInfo();
		dataRecordInfo.setAlertInfoId(alertInfo.getAlertInfoId());
		dataRecordInfo.setCertificateCode(personInfo.getCertificateCode());
		dataRecordInfoMapper.updateAlertInfoIdByCertificateCode(dataRecordInfo);
	}
    
}